import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ObjectNotActive;
import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import common.CommonTools;


public class LocalServer {

	public static void main(String[] args) {
		
		String serverGeolocation = args[0];
		ORB orb = ORB.init(args, null);		
		
		try {
			// what's the need for using narrow?
			POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			
			PlayerServiceImpl playerServiceImpl = new PlayerServiceImpl(args[0]);
			byte[] playerServiceImplId = rootPOA.activate_object(playerServiceImpl);
			org.omg.CORBA.Object playerServiceRef = rootPOA.id_to_reference(playerServiceImplId);
			String iorPlayerServiceImpl  = orb.object_to_string(playerServiceRef);
			
			PrintWriter playerServiceImplFile = new PrintWriter(CommonTools.getIORFileName("player","server.properties",serverGeolocation));
			playerServiceImplFile.println(iorPlayerServiceImpl);
			playerServiceImplFile.close();
			
			// CORBA Administrator part
			AdministratorServiceImpl administratorService = new AdministratorServiceImpl(serverGeolocation);
			
			byte[] administratorServiceImplId = rootPOA.activate_object(administratorService);
			org.omg.CORBA.Object administratorServiceRef = rootPOA.id_to_reference(administratorServiceImplId);
			String iorAdministratorServiceRef  = orb.object_to_string(administratorServiceRef);
			
			PrintWriter administratorServiceFile = new PrintWriter(CommonTools.getIORFileName("admin","server.properties",serverGeolocation));
			administratorServiceFile.println(iorAdministratorServiceRef);
			administratorServiceFile.close();
			
			rootPOA.the_POAManager().activate();
			
			CommonTools.writeLog("server", serverGeolocation,
					"Administrator CORBA service started at: "
							+ serverGeolocation + " : "
							, "server");
						
			// UDP Administrator part
			CommonTools.writeLog(
					"server",
					serverGeolocation,
					"Administrator UDP service started at: "
							+ serverGeolocation
							+ " server on the port: "
							+ CommonTools.getproperty("server.properties",
									"UDPPort_" + serverGeolocation), "server");
			
			// use a thread to run the UDP server
			UDPTools.UDPServer(Integer.parseInt(CommonTools.getproperty(
					"server.properties", "UDPPort_" + serverGeolocation)),
					serverGeolocation);
			
			
			orb.run();
			
			
		} catch (InvalidName e) {
			e.printStackTrace();
		} catch (ServantAlreadyActive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WrongPolicy e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ObjectNotActive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AdapterInactive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		

	}

}
